﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnitySampleAssets.CrossPlatformInput;

public class Joystick : MonoBehaviour , IPointerUpHandler , IPointerDownHandler , IDragHandler {

    public int MovementRange = 100;

    public enum AxisOption
    {                                                    // Options for which axes to use                                                     
        Both,                                                                   // Use both
        OnlyHorizontal,                                                         // Only horizontal
        OnlyVertical                                                            // Only vertical
    }

    public AxisOption axesToUse = AxisOption.Both;   // The options for the axes that the still will use
    public string horizontalAxisName = "Horizontal";// The name given to the horizontal axis for the cross platform input
    public string verticalAxisName = "Vertical";    // The name given to the vertical axis for the cross platform input 

    private Vector3 startPos;
    private bool useX;                                                          // Toggle for using the x axis
    private bool useY;                                                          // Toggle for using the Y axis
    private CrossPlatformInputManager.VirtualAxis horizontalVirtualAxis;               // Reference to the joystick in the cross platform input
    private CrossPlatformInputManager.VirtualAxis verticalVirtualAxis;                 // Reference to the joystick in the cross platform input
    RectTransform rectTrans;
    RectTransform panelrectTrans;
    void OnEnable () {
        if (UnitySampleAssets.CrossPlatformInput.CrossPlatformInputManager.IsMobileInput)
        {
            startPos = transform.localPosition;
            CreateVirtualAxes();
        }
        rectTrans = GetComponent<RectTransform>();
        panelrectTrans = rectTrans.parent.GetComponent<RectTransform>();
    }

    private void UpdateVirtualAxes (Vector3 value)
    {
        var delta = value - startPos ;
        //delta /= MovementRange;
        if(useX)
        horizontalVirtualAxis.Update (delta.x);

        if(useY)
        verticalVirtualAxis.Update (delta.y); 
    }

    private void CreateVirtualAxes()
    {
        // set axes to use
        useX = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyHorizontal);
        useY = (axesToUse == AxisOption.Both || axesToUse == AxisOption.OnlyVertical);

        // create new axes based on axes to use
        if (useX)
            horizontalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(horizontalAxisName);
        if (useY)
            verticalVirtualAxis = new CrossPlatformInputManager.VirtualAxis(verticalAxisName);
    }


    public  void OnDrag(PointerEventData data) {
        Vector2 currPos;
        RectTransformUtility.ScreenPointToLocalPointInRectangle(panelrectTrans, data.position, data.pressEventCamera, out currPos); 

        if (useX) {
            int delta = (int) (currPos.x- startPos.x);
            delta = Mathf.Clamp(delta,  - MovementRange,  MovementRange);
            currPos.x = delta;
        }

        if (useY)
        {
            int delta = (int)(currPos.y - startPos.y);
            delta = Mathf.Clamp(delta, -MovementRange,  MovementRange);
            currPos.y = delta;
        }
        rectTrans.localPosition = new Vector3(startPos.x + currPos.x , startPos.y + currPos.y , startPos.z);
        UpdateVirtualAxes (transform.localPosition); 
    }


    public  void OnPointerUp(PointerEventData data)
    {
        rectTrans.localPosition = startPos;
        UpdateVirtualAxes (startPos);
        Debug.Log(data);
    }


    public  void OnPointerDown (PointerEventData data)
    {
        Debug.Log(data);
    }

    void OnDisable () {
        // remove the joysticks from the cross platform input
        if (useX)
        {
            horizontalVirtualAxis.Remove();
        }
        if (useY)
        {
            verticalVirtualAxis.Remove();
        }
    }
}
